<?php

/***  Documentation

    Greqo Blogger Unit Test (requiring account login)
    
    Revision: $Rev$
    Last Update: $Date: 2009-05-23 21:10:24 +0000 (Sat, 23 May 2009) $
    Author: Tom at klenwell@gmail.com
  
NOTES
    This test requires conf/login.php be set with valid Google account info
    for user with at least 1 Blogger blog
    
    NOTE: it may change the current state of your blog.  It tests by adding,
    editing, deleting posts.  Although it tries to clean up after itself, it
    may be necessary to manually delete posts in the event of failures.

______________________________________________________________________________*/

$dn1 = dirname;
$ds = DIRECTORY_SEPARATOR;
$libdir = $dn1($dn1($dn1(__FILE__))) . $ds . 'lib' . $ds;
$grqdir = $dn1($libdir) . $ds . 'greqo' . $ds;
require_once($libdir . "simpletest{$ds}unit_tester.php");
require_once($grqdir . "blogger.class.php");

class BloggerRequireLoginTestCase extends UnitTestCase {
    
    var $debug = 0;
    var $is_set_up_once = 0;
    var $blogger_blog_id = '2399953';
    
    function setUp() 
    {
        $this->setUpOnce();    
    }
    
    function tearDown() 
    {
    }
    
    function setUpOnce()
    {
        if ( $this->is_set_up_once ) return;      
        
        // set up
        $utime = microtime();
        $this->start_timer = substr($utime,0,9) + substr($utime,-10);
        $this->is_set_up_once = 1;
        
        // Greqo class
        $this->Blogger = new GreqoBlogger($start_session=0);
        
        // set BlogList
        $this->assertTrue($this->Blogger->request_blog_list());
        
        // pick random blog id
        $this->BlogInfo = $this->Blogger->BlogList['ITEM'][
            array_rand($this->Blogger->BlogList['ITEM'])
        ];
    }
    
    

    /* TEST METHODS */
    function testTest() 
    {
        $this->assertTrue(1);
    }
    
    function testInstance()
    {        
        $this->assertTrue(is_a($this->Blogger, 'GreqoBlogger'));
    }
    
    function testIsAuthRequestPrepared()
    {
        $this->assertTrue($this->Blogger->_is_auth_request_prepared());
    }
    
    function testParseBlogId()
    {
        // $this->assertEqual($this->BlogInfo['blog_id'] from setUpOnce
        $this->assertEqual($this->BlogInfo['blog_id'],
                substr($this->BlogInfo['id'],
                       0-(strlen($this->BlogInfo['blog_id'])) ) );
    }
    
    function testRequestBloggerFeedBasic()
    {
        /* requests Blogger's blog's feed */
        $FeedArray = $this->Blogger->request_feed($this->blogger_blog_id);
        $this->assertEqual($FeedArray['CHANNEL'][0], 'tag:blogger.com,1999:blog-2399953');
        $this->assertEqual(count($FeedArray['ITEM']), 25);
        #$this->pr($FeedArray);
    }
    
    function testRequestBloggerFeedQuery()
    {
        $Query = array(
            'published-min' => '2008-01-01',
            'published-max' => '2008-12-31',
            'orderby'       => 'published',
        );
        $FeedArray = $this->Blogger->request_feed($this->blogger_blog_id, $Query);
        $this->assertEqual($FeedArray['ITEM'][0]['published'], '2008-12-10T18:00:00.000-08:00');
        #$this->pr($FeedArray);
    }    
    
    function testRequestBlogList()
    {
        #$this->assertTrue($this->Blogger->request_blog_list()); -> see setUpOnce
        $this->assertTrue( isset($this->Blogger->BlogList['CHANNEL']) );
        $this->assertTrue( isset($this->Blogger->BlogList['ITEM']) );
        $this->assertTrue( isset($this->Blogger->BlogList['ITEM'][0]['id']) );
        $this->assertTrue( is_array($this->Blogger->BlogList['ID']) );
        #$this->pr($this->Blogger->BlogList['ID']);
    }
    
    function testPostAndUpdateAndDeleteEntry()
    {
        $BlogInfo = $this->Blogger->BlogList['ITEM'][0];

        $title = 'Greqo 4 Test';
        $body = <<<XHTML
<p>This is a test of the Greqo 4 Blogger Class</p>
<p>For more info, see <a href="http://code.google.com/p/greqo/">
http://code.google.com/p/greqo/</a></p>
<p>this post is scheduled for deletion</p>
XHTML;
        $OptsDict['blog_id'] = $BlogInfo['blog_id'];
        $OptsDict['categories'] = 'greqo, test';
        
        $PostResponse = $this->Blogger->post_entry($body, $title, $OptsDict);
        $this->assertTrue(!empty($PostResponse['id']));
        
        // update post and test exists
        $title .= ' <em>updated</em>';
        $body .= sprintf("\n<h6>updated at %s</h6>", date('Y-m-d H:i:s'));
        $UpdateResponse = $this->Blogger->edit_entry($PostResponse['id_num'],
            $body, $title, $OptsDict);
        $this->assertTrue(!empty($UpdateResponse['id']));
        $this->Blogger->get_url($UpdateResponse['links']['view']);
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 2);
        
        // delete post and test removed
        $this->assertTrue($this->Blogger->delete_entry($PostResponse['id_num'],
            $OptsDict));
        $this->Blogger->get_url($PostResponse['links']['view']);
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 4);
        
        #$this->pr(array($PostResponse, $UpdateResponse));
    }
    
    function testPostAndGetAndDeleteComment()
    {
        $BlogInfo = $this->Blogger->BlogList['ITEM'][0];
        $FeedArray = $this->Blogger->request_feed($BlogInfo['blog_id']);
        $post_id = $FeedArray['ITEM'][0]['id_num'];
        
        // add comment
        $comment = sprintf('A test of Greqo4 comment API at %s', date('H:i:s'));
        $CommentResponse = $this->Blogger->post_comment($post_id, $comment);
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 2);
        $this->assertTrue( isset($CommentResponse['published']) );
        
        // get comments for post
        $CommentList = $this->Blogger->get_comments_by_post($post_id);
        $this->assertEqual($CommentList['ITEM'][0]['id_num'], $CommentResponse['id_num']);
        
        // delete comment
        $comment_id = $CommentResponse['id_num'];
        $this->assertTrue($this->Blogger->delete_comment($comment_id, $post_id));
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 2);

        #$this->pr(array($CommentResponse, $CommentList, $FeedArray));
    }
    
    function testGetAllComments()
    {
        $BlogInfo = $this->Blogger->BlogList['ITEM'][0];
        $FeedArray = $this->Blogger->request_feed($BlogInfo['blog_id']);
        $post_id = $FeedArray['ITEM'][0]['id_num'];
        
        // add comment
        $comment = sprintf('Greqo4 comment for TestGetAllComments test at %s', date('H:i:s'));
        $CommentResponse = $this->Blogger->post_comment($post_id, $comment);
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 2);
        $this->assertTrue( isset($CommentResponse['published']) );
               
        $CommentList = $this->Blogger->get_comments();
        $this->assertEqual($CommentList['ITEM'][0]['id_num'], $CommentResponse['id_num']);
        
        // delete comment
        $comment_id = $CommentResponse['id_num'];
        $this->assertTrue($this->Blogger->delete_comment($comment_id, $post_id));
        $this->assertEqual($this->Blogger->GreqoResponse->code_class, 2);
        
        #$this->pr($CommentList);
    }
    
    
    
    
    /* HELPER METHODS */
    function get_script_time()
    {
        $utime = microtime();
        return number_format((substr($utime,0,9) + substr($utime,-10)) - $this->start_timer, 4);
    }

    function kill($html, $message='dying')
    {
        $this->print_d(htmlspecialchars($html), '#600');
        print $html;
        die($message);
    }
    
    function echo_d($message, $color='#ccc')
    {
        $_D = debug_backtrace();
        $f = basename($_D[0]['file']);
        $l = $_D[0]['line'];
        $loc = "{$f}:{$l}";
        $out = "<div style='line-height:1.5em; font-family:monospace; color:$color;'>$message <span style='color:#666;'>[$loc]</span></div>";
        $this->_DBG[] = "$loc -> " . strip_tags($message);
        echo $out;
        return;
    }
    
    function print_d($message, $color='#c33')
    {
        if ( $this->debug ) $this->echo_d($message, $color);
        return;
    }
    
    function print_r($Mixed)
    {
        $return = htmlspecialchars(print_r($Mixed, 1));
        $return = "<pre>$return</pre>";
        return $return;
    }
    
    function pr($Mixed, $header='')
    {
        if ( !empty($header) ) $header = "<b>$header</b>";
        $return = htmlspecialchars(print_r($Mixed, 1));
        $return = "<pre style='border:1px solid #ccc; background:#f3f3f3; padding:4px;'>$header\n\n$return</pre>";
        $this->echo_d($return, '#333');
    }
    
    function dump()
    {
        echo $this->print_r($this);
        return;
    }
}


$Test = &new BloggerRequireLoginTestCase();
$Test->run(new HtmlReporter());

$Test->echo_d('test complete in '.$Test->get_script_time().' s', '#600');
?>
